#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use File::Copy;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('arg-parsing');

my $n_tests = 25;

$td->runtest("required argument",
             {$td->COMMAND => "qpdf --password minimal.pdf"},
             {$td->REGEXP => "must be given as --password=pass",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("required argument with choices",
             {$td->COMMAND => "qpdf --decode-level minimal.pdf"},
             {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("required argument with choices",
             {$td->COMMAND => "qpdf --decode-level minimal.pdf"},
             {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
copy("minimal.pdf", '@file.pdf');
$td->runtest("\@file exists and file doesn't",
             {$td->COMMAND => "qpdf --check \@file.pdf"},
             {$td->FILE => "check-at-file.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("missing underlay filename",
             {$td->COMMAND => "qpdf --underlay --"},
             {$td->REGEXP => ".*underlay file not specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("extra overlay filename",
             {$td->COMMAND => "qpdf --overlay x x --"},
             {$td->REGEXP => ".*overlay file already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("multiple pages options",
             {$td->COMMAND => "qpdf --pages . --password=x -- --pages . --"},
             {$td->REGEXP => ".*--pages may only be specified one time.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("bad numeric range detects unclosed --pages",
             {$td->COMMAND => "qpdf --pages . --pages . --"},
             {$td->REGEXP => ".*pages options must be terminated with --.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("bad file detected as unclosed --pages",
             {$td->COMMAND => "qpdf --pages . 1 --xyz out"},
             {$td->REGEXP => ".*pages options must be terminated with --.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("misplaced pages range",
             {$td->COMMAND => "qpdf --pages --range=1 . --password=z --"},
             {$td->REGEXP => ".*range must follow a file name.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("duplicate pages range",
             {$td->COMMAND => "qpdf --pages --file=." .
                  " --range=1 --range=2 . --password=z --"},
             {$td->REGEXP => ".*range already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("misplaced pages password",
             {$td->COMMAND => "qpdf --pages --password=z . 1 --"},
             {$td->REGEXP => ".*password must follow a file name.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("duplicated pages password",
             {$td->COMMAND => "qpdf --pages . --password=z --password=z --"},
             {$td->REGEXP => ".*password already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v1-only objects json-key",
             {$td->COMMAND => "qpdf --json=2 --json-key=objects minimal.pdf"},
             {$td->REGEXP => ".*\"objects\" and \"objectinfo\" are " .
                  "only valid for json version 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v1-only objectinfo json-key",
             {$td->COMMAND => "qpdf --json=2 --json-key=objectinfo minimal.pdf"},
             {$td->REGEXP => ".*\"objects\" and \"objectinfo\" are " .
                  "only valid for json version 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v2-only qpdf json-key",
             {$td->COMMAND => "qpdf --json=1 --json-key=qpdf minimal.pdf"},
             {$td->REGEXP => ".*\"qpdf\" is only valid for json version > 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("wrong number of collate args",
             {$td->COMMAND =>
                  "qpdf --collate=2,3 collate-odd.pdf" .
                  " --pages . minimal.pdf collate-even.pdf -- a.pdf"},
             {$td->REGEXP => ".*--collate has more than one value.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("wrong number of collate args",
             {$td->COMMAND =>
                  "qpdf collate-odd.pdf --pages . minimal.pdf collate-even.pdf --" .
                  " --collate=2,3 a.pdf"},
             {$td->REGEXP => ".*--collate has more than one value.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

# Ignoring -- at the top level was never intended but turned out to
# have been there for a long time so that people relied on it. It is
# intentionally not documented.
$td->runtest("ignore -- at top level",
             {$td->COMMAND => "qpdf -- --check -- minimal.pdf --"},
             {$td->REGEXP => ".*No syntax or stream encoding errors found.*",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("empty and replace-input",
             {$td->COMMAND => "qpdf --empty --replace-input"},
             {$td->REGEXP => ".*--replace-input may not be used with --empty.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("missing key length",
             {$td->COMMAND => "qpdf --encrypt --"},
             {$td->REGEXP => ".*encryption key length is required",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

# Disallow mixing positional and flag-style encryption arguments.
my @bad_enc = (
    "u --owner-password=x",
    "u o --bits=128",
    "--user-password=u o",
    "--user-password=u --owner-password=o 256",
    );
foreach my $arg (@bad_enc)
{
    $td->runtest("mixed encryption args ($arg)",
                 {$td->COMMAND => "qpdf --encrypt $arg"},
                 {$td->REGEXP => ".*positional and dashed encryption arguments may not be mixed",
                      $td->EXIT_STATUS => 2},
                 $td->NORMALIZE_NEWLINES);
}

cleanup();
$td->report($n_tests);
